import {
  Controller,
  Req,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Session,
} from '@nestjs/common';
import { CityService } from './city.service';
import { CreateCityDto } from './dto/create-city.dto';
import { UpdateCityDto } from './dto/update-city.dto';

@Controller('city')
export class CityController {
  constructor(private readonly cityService: CityService) {}

  @Post()
  create(@Body() createCityDto: CreateCityDto, @Session() session, @Req() req) {
    // return this.cityService.create(createCityDto);
    console.log('======session=====');
    console.log(session);
    console.log(session.count);

    const referer = req.headers['referer'] || 'No referer provided';
    console.log('=====referer=====');
    console.log(req.headers);
    console.log(referer);
    session.count = session.count ? session.count + 1 : 1;
    return session.count;
  }

  @Get()
  findAll() {
    return this.cityService.findAll();
  }

  @Get(':id')
  findOne(@Session() session, @Req() req, @Param('id') id: string) {
    console.log('======session=====');
    console.log(session);
    console.log(session.count);

    const referer = req.headers['referer'] || 'No referer provided';
    console.log('=====referer=====');
    console.log(req.headers);
    console.log(referer);
    session.count = session.count ? session.count + 1 : 1;
    return session.count;
    // return this.cityService.findOne(+id);
  }

  @Patch(':id')
  update(@Param('id') id: string, @Body() updateCityDto: UpdateCityDto) {
    return this.cityService.update(+id, updateCityDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.cityService.remove(+id);
  }
}
